package com.netcross.myapplication.ui.notifications;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.netcross.myapplication.LoginActivity;
import com.netcross.myapplication.R;
import com.netcross.myapplication.databinding.FragmentNotificationsBinding;

public class NotificationsFragment extends Fragment {
    SharedPreferences sp;
    private NotificationsViewModel notificationsViewModel;
    private FragmentNotificationsBinding binding;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        notificationsViewModel =
                new ViewModelProvider(this).get(NotificationsViewModel.class);

        binding = FragmentNotificationsBinding.inflate(inflater, container, false);
        View root = binding.getRoot();


        sp = getActivity().getSharedPreferences("config", Activity.MODE_PRIVATE);
         SharedPreferences.Editor editor = sp.edit();
         String getUserName = sp.getString("userName","");
        String getPassword = sp.getString("password","");
        String getIp = sp.getString("ip","");
        String getAndroidId = sp.getString("androidId","");
        String getRememberMe = sp.getString("rememberMe","");

        TextView tv1 = root.findViewById(R.id.showUserNameTextView);
        tv1.setText(getUserName);
        TextView tv2 = root.findViewById(R.id.textView11);
        if (getUserName.equals("user1")){
          String  message= "该内容只有超级用户可见";
          tv2.setText(message);
            }
        else {
            tv2.setVisibility(View.INVISIBLE);
        }



        return root;
    }




 @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

     Button button = (Button) getActivity().findViewById(R.id.logoutButton);
     button.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
             sp = getActivity().getSharedPreferences("config", Activity.MODE_PRIVATE);
             SharedPreferences.Editor  editor = sp.edit();
             editor.putString("userName","");
             editor.putString("password","");
             editor.putString("ip","");
             editor.putString("androidId","");
             editor.putString("rememberMe", "False");
             editor.commit();
             goToLogin();


         }
     });
        }


        private void goToLogin(){
         Intent intent = new Intent(getActivity(), LoginActivity.class);
         this.startActivity(intent);
        }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}